<template>
    <div class="permission-wrapper">
        <div class="permission">

            <!--数据表格-->
            <el-table 
                ref="table"
                :data="tableData" 
                stripe 
                border
            >

                <el-table-column prop="id" label="id" width="80" sortable />
                <el-table-column prop="name" label="name" />
                <el-table-column prop="method" label="method" />

                <el-table-column fixed="right" label="Operations" width="120">
                    <template #default="scope">
                        <el-button link type="primary" size="small" @click="openUpdateForm(scope.row)">修改</el-button>

                        <el-popconfirm title="确定要删除吗?" @confirm="deleteItem(scope.row.id)">
                            <template #reference>
                                <el-button link type="primary" size="small">删除</el-button>
                            </template>
                        </el-popconfirm>
                    </template>
                </el-table-column>

            </el-table>

            <!--添加按钮-->
            <el-button 
                @click="addFormVisible = true"
                style="width: 100%; margin: 15px 0;">
                添加
            </el-button>

            <!--分页器-->
            <el-pagination
                v-model:currentPage="currentPage"
                v-model:page-size="pageSize"
                :page-sizes="[5, 10, 20, 30, 40, 50, 100, 200]"
                :total="total" 
                background
                layout="total, sizes, prev, pager, next, jumper"
                style="justify-content: center;" 
                @size-change="permissionByPage()"
                @current-change="permissionByPage()"
                @prev-click="permissionByPage()"
                @next-click="permissionByPage()" />

            <!--添加表单-->
            <el-dialog 
                v-model="addFormVisible"
                @close="handleClose"
                title="添加权限">

                <el-form 
                    @submit.prevent
                    :model="formData"
                    :rules="rules"
                    ref="addRuleFormRef"
                    label-width="120px">

                    <el-form-item label="name" prop="name">
                        <el-input v-model="formData.name" />
                    </el-form-item>

                    <el-form-item label="method" prop="method">
                        <el-select
                            v-model="formData.method"
                            placeholder="method"
                            style="width: 240px"
                        >
                            <el-option
                                label="POST"
                                value="POST"
                            />
                            <el-option
                                label="DELETE"
                                value="DELETE"
                            />
                            <el-option
                                label="PUT"
                                value="PUT"
                            />
                            <el-option
                                label="GET"
                                value="GET"
                            />
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submit('POST', $refs.addRuleFormRef)">提交</el-button>
                    </el-form-item>

                </el-form>

            </el-dialog>

            <!--修改表单-->
            <el-dialog 
                v-model="updateFormVisible"
                @close="handleClose"
                title="修改权限">

                <el-form 
                    @submit.prevent
                    :model="formData"
                    :rules="rules"
                    ref="updateRuleFormRef"
                    label-width="120px">

                    <el-form-item label="id" prop="id" >
                        <el-input v-model="formData.id" disabled />
                    </el-form-item>
                    <el-form-item label="name" prop="name">
                        <el-input v-model="formData.name" />
                    </el-form-item>

                    <el-form-item label="method" prop="method">
                        <el-select
                            v-model="formData.method"
                            placeholder="method"
                            style="width: 240px"
                        >
                            <el-option
                                label="POST"
                                value="POST"
                            />
                            <el-option
                                label="DELETE"
                                value="DELETE"
                            />
                            <el-option
                                label="PUT"
                                value="PUT"
                            />
                            <el-option
                                label="GET"
                                value="GET"
                            />
                        </el-select>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="submit('PUT', $refs.updateRuleFormRef)">提交</el-button>
                    </el-form-item>

                </el-form>

            </el-dialog>

        </div>
    </div>
</template>

<script>
import { nextTick } from '@vue/runtime-core'
import { setPermission, getPermissionByPage, getPermissionTotal } from '@/api/permission.js'

export default {
    name: 'permission',
    data() {
        return {
            currentPage: 1,
            pageSize: 10,
            total: 200,
            tableData: null,
            addFormVisible: false,
            updateFormVisible: false,
            formData: {
                id: null,
                name: null,
                method: null
            },
            rules: {
                name: [
                    { required: true, message: '不能为空', trigger: 'blur' },
                    { max: 64, message: '最大长度为64', trigger: 'blur' },
                ],
                method: [
                    { required: true, message: '不能为空', trigger: 'change' },
                ]
            }
        }
    },
    
    methods: {
        //关闭弹出表单
        handleClose () {
            for (const key of Reflect.ownKeys(this.formData)) {
                this.formData[key] = null
            }
        },
        //打开更新表单
        openUpdateForm (row) {
            this.updateFormVisible = true

           //最简单的深拷贝
           this.formData = JSON.parse(JSON.stringify(row))
        },

        //增改
        submit (method, formRef) {
            formRef.validate(async (valid, fields) => {
                if (valid) {
                    this.addFormVisible = false
                    this.updateFormVisible = false
                    let successMsg = method === 'POST' ? '添加成功' : '修改成功'

                    let result = await setPermission(this.formData, method)
                    if (result instanceof Error) return ElMessage.error(result.message)

                    ElMessage.success(successMsg)
                    this.permissionByPage()
                }
            })
        },
        //删
        async deleteItem (id) {
            let result = await setPermission({ id }, 'DELETE')
            if (result instanceof Error) return ElMessage.error(result.message)

            ElMessage.success('删除成功')
            this.permissionByPage()
        },
        //查
        async permissionByPage () {
            let result1 = await getPermissionByPage(this.currentPage, this.pageSize)
            if (result1 instanceof Error) return ElMessage.error(result1.message)

            let result2 = await getPermissionTotal()
            if (result2 instanceof Error) return ElMessage.error(result2.message)
            
            this.tableData = result1
            this.total = result2.total

            nextTick(() => {
                this.$refs.table.sort('id', 'ascending')
            })
        },
    },
    
    created () {
        this.permissionByPage()
    }
}
</script>

<style lang="less" scoped>
.permission{
    padding: 20px;
}
</style>